﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace com.haoest.QrPortal
{
    public class DeviceCollection : CollectionBase
    {
        private DataTable dt = new DataTable("DeviceDateTable");
        public DeviceCollection()
        {
            dt.Columns.Add("id");
            dt.Columns.Add("name");
        }

        public void Add(Device device)
        {
            InnerList.Add(device);        //添加
            DataRow dr = dt.NewRow();
            dr["id"] = device.Id;
            dr["name"] = device.Name;
            dt.Rows.Add(dr);
        }

        public void Remove(Device device)
        {
            InnerList.Remove(device);     //删除
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["id"].ToString() == device.Id)
                {
                    dt.Rows.Remove(dr);
                    break;
                }
            }
        }

        public void Update(Device device)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["id"].ToString() == device.Id)
                {
                    dr["name"] = device.Name;
                    break;
                }
            }
        }

        public int Count()
        {
            return InnerList.Count;     //返回元素个数
        }

        public void Clear()
        {
            InnerList.Clear();          //全部清除
            dt.Rows.Clear();
        }

        public DataTable GetDataTable()
        {
            return dt;
        }
    }
}
